package com.eleven.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.eleven.pojo.Letter;
import com.eleven.pojo.Msg;
import com.eleven.service.LetterService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Controller
public class LetterController {

	@Autowired
	LetterService letterService;

	public LetterService getLetterService() {
		return letterService;
	}

	public void setLetterService(LetterService letterService) {
		this.letterService = letterService;
	}
	
	/**
	 * 查看自己的私信(包括有收到的私信和发出的私信)
	 */
	@RequestMapping(value="/getMyLetter/{id}",method=RequestMethod.GET)
	@ResponseBody
	public Msg getMyLetter(@RequestParam(value="pn",defaultValue="1")Integer pn,@PathVariable("id") Integer id) {
		PageHelper.startPage(pn, 10);//传入页码和每页显示的数量
		List<Letter> letter = letterService.getMyLetter(id);

		PageInfo pageInfo = new PageInfo(letter, 5);//5是连续显示5页的内容
		if(letter.size() > 0) {
			return Msg.success().add("pageInfo", pageInfo);
		}else {
			return Msg.fail().add("message", "暂无私信");
		}
		
	}
	
	/**
	 * 回复私信(发出私信)
	 */
	
	
	/**
	 * 查看全部私信
	 */
	@RequestMapping(value="/getAllLetter",method=RequestMethod.GET)
	@ResponseBody
	public Msg getLetter(@RequestParam(value="pn",defaultValue="1")Integer pn) {
		//引入pageHelpers
		PageHelper.startPage(pn, 10);
		List<Letter> letter=letterService.getAllLetter();
		PageInfo pageInfo=new PageInfo(letter,5);
		if(letter.size() > 0) {
			return Msg.success().add("pageInfo", pageInfo);
		}else {
			return Msg.fail().add("message", "暂无私信");
		}
	}
	/**
	 * 查看全部私信降序
	 */
	@RequestMapping(value="/getAllLetterTime",method=RequestMethod.GET)
	@ResponseBody
	public Msg getAllLetterTime(@RequestParam(value="pn",defaultValue="1")Integer pn) {
		//引入pageHelpers
		PageHelper.startPage(pn, 10);
		List<Letter> letter=letterService.getAllLetterTime();
		PageInfo pageInfo=new PageInfo(letter,5);
		if(letter.size() > 0) {
			return Msg.success().add("pageInfo", pageInfo);
		}else {
			return Msg.fail().add("message", "暂无私信");
		}
	}
}
